package com.chengyu.core.service.member.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.chengyu.core.mapper.BaseMapper;
import com.chengyu.core.mapper.PmsServicesMapper;
import com.chengyu.core.mapper.UmsMemberViewServicesMapper;
import com.chengyu.core.model.PmsServices;
import com.chengyu.core.model.UmsMember;
import com.chengyu.core.model.UmsMemberViewServices;
import com.chengyu.core.model.UmsMemberViewServicesExample;
import com.chengyu.core.service.member.MemberViewServicesService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @title  浏览商品
 * @author LeGreen
 * @date   2022/4/28
 */
@Service
public class MemberViewServicesServiceImpl implements MemberViewServicesService {
	
	@Autowired
	private UmsMemberViewServicesMapper memberViewServicesMapper;
	@Autowired
	private PmsServicesMapper servicesMapper;
	@Autowired
	private BaseMapper baseMapper;

	@Override
	public List<UmsMemberViewServices> getMemberViewServicesList(Integer memberId, Date date, Integer page, Integer pageSize) {
		PageHelper.startPage(page, pageSize);

		UmsMemberViewServicesExample example = new UmsMemberViewServicesExample();
		example.setOrderByClause("add_time desc");
		UmsMemberViewServicesExample.Criteria criteria = example.createCriteria();
		if(memberId != null){
			criteria.andMemberIdEqualTo(memberId);
		}
		if(date != null){
			criteria.andDateEqualTo(date);
		}
		return memberViewServicesMapper.selectByExample(example);
	}

	@Override
	public void viewServices(UmsMember member, Integer servicesId) {
		//增加商品的点击数量
		baseMapper.update("update pms_services set point_num=point_num+1 where id = "+servicesId);
		if(member == null){
			return ;
		}
		UmsMemberViewServicesExample example = new UmsMemberViewServicesExample();
		example.setOrderByClause("upd_time desc");
		example.createCriteria().andMemberIdEqualTo(member.getId()).andServicesIdEqualTo(servicesId).andDateEqualTo(DateUtil.date());
		List<UmsMemberViewServices> list = memberViewServicesMapper.selectByExample(example);
		if(CollectionUtil.isNotEmpty(list)){
			//更新浏览时间
			UmsMemberViewServices updateViewServices = new UmsMemberViewServices();
			updateViewServices.setId(list.get(0).getId());
			updateViewServices.setUpdTime(DateUtil.date());
			updateViewServices.setDate(DateUtil.date());
			memberViewServicesMapper.updateByPrimaryKeySelective(updateViewServices);
		}else{
			UmsMemberViewServices viewServices = new UmsMemberViewServices();
			viewServices.setMemberId(member.getId());
			viewServices.setMemberName(member.getNickname());
			viewServices.setHeadImg(member.getHeadImg());
			viewServices.setDate(DateUtil.date());

			PmsServices services = servicesMapper.selectByPrimaryKey(servicesId);
			viewServices.setServicesId(services.getId());
			viewServices.setServicesName(services.getTitle());
			viewServices.setServicesMainImg(services.getMainImg());
			viewServices.setServicesCateId(services.getCatePid());
			viewServices.setPrice(services.getPrice());
			viewServices.setAddTime(DateUtil.date());
			viewServices.setUpdTime(viewServices.getAddTime());
			memberViewServicesMapper.insert(viewServices);
		}
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void deleteViewServices(Integer viewServicesId) {
		memberViewServicesMapper.deleteByPrimaryKey(viewServicesId);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void deleteViewServicesByMemberId(Integer memberId) {
		UmsMemberViewServicesExample example = new UmsMemberViewServicesExample();
		example.createCriteria().andMemberIdEqualTo(memberId);
		memberViewServicesMapper.deleteByExample(example);
	}

	@Override
	public List<Integer> getMemberViewServicesCateIdList(Integer memberId) {
		Date now = DateUtil.date();
		UmsMemberViewServicesExample example = new UmsMemberViewServicesExample();
		example.createCriteria().andMemberIdEqualTo(memberId).andDateBetween(DateUtil.offsetDay(now, -90), now);
		List<UmsMemberViewServices> list = memberViewServicesMapper.selectByExample(example);
		if(CollectionUtil.isEmpty(list)){
			return null;
		}
		return list.stream().map(UmsMemberViewServices::getServicesCateId).distinct().collect(Collectors.toList());
	}

}